<?php
class Friend_model extends CI_Model {

	function __construct()
    {
        parent::__construct();
    }
	/**
	 * 新增一张点卡，code随机
	 */
	public function add($from_user, $to_user) {   
		$data = array('created' => date("Y-m-d H:i:s"), 'from_user' => $from_user, 'to_user' => $to_user);
		$this->db->insert("friends", $data);
		return $this->db->insert_id();
	}
	
	public function remove($from_user, $to_user) {
		$sql = " delete from friends where from_user=" . intval($from_user) . " and to_user=" . $to_user;
		$this->db->query($sql);
	}
	
	public function find_friends($user_id) {
		$sql = " select u.id,u.nickname,u.email from friends f left join users u on u.id = f.to_user
		where f.from_user=" . $user_id;
		return $this->db->query($sql)->result_array();
	}
	/**
	 * 查询用户俩是否朋友
	 * @param int $from_user
	 * @param int $to_user
	 */
	public function is_friend($from_user, $to_user) {
		$this->db->where('from_user', intval($from_user));
		$this->db->where('to_user', intval($to_user));
		$this->db->from('friends');
		return $this->db->count_all_results();
	}
}